class StoreController < ApplicationController
	layout 'main'
	
	# 商家自己disable的，管理员没有passed的不能列出
	def list
		# 默认为10个一页
		@page, @total_store, @total_page = paging(10)
		@stores = Store.find(:all, :conditions => 'disabled = false AND passed = true', :limit => 10, :offset => @page - 1)
	end
	
	# 商家自己disable的，管理员没有passed的不能列出
	def list_by_condition
		# 默认为10个一页
		search_name = params[:search_name]
		@page, @total_store, @total_page = paging(10)
		@stores = Store.find(:all, :conditions => [ 'name like ? AND disabled = false AND passed = true', '%' + search_name + '%'], :limit => 10, :offset => @page - 1)
	end
	
	def show_store
		store_id = params[:store_id]
		@store = Store.find(store_id)
		@merchant = @store.merchant
	end
	
	private
	def paging(size)
		page = params[:page].nil? ? 1 : params[:page].to_i
		total_store = Store.count(:conditions => 'disabled = false AND passed = true')
		last_page = total_store % size == 0 ? 0 : 1
		total_page = total_store / size + last_page
		return page, total_store, total_page
	end
end